﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Bpms.Data;
using Bpms.Entity;
using Bpms.Log;
using Bpms.Cache;

namespace Bpms.Bll
{
    public class JobTitle
    {
        #region Member
        /// <summary>
        /// 数据库改动事件委托
        /// </summary>
        public delegate void DBChangedEventHandler();

        /// <summary>
        /// 日志事件委托
        /// </summary>
        public delegate void LogEventHandler();

        /// <summary>
        /// 数据库改动事件
        /// </summary>
        public static event DBChangedEventHandler DBChanged;

        /// <summary>
        /// 写日志事件
        /// </summary>
        public static event LogEventHandler WriteLog;

        #region Private

        private static IDataProvider provider = DatabaseProvider.GetInstance();

        /// <summary>
        /// 数据改动事件处理函数
        /// </summary>
        static void OnDBChanged()
        {
            if (DBChanged != null)
            {
                DBChanged();
            }
        }

        /// <summary>
        /// 写日志处理函数
        /// </summary>
        static void OnWriteLog()
        {
            if (WriteLog != null)
            {
                WriteLog();
            }
        }

        #endregion

        #endregion

        /// <summary>
        /// 添加职位
        /// </summary>
        /// <param name="jobTitle">职位实体对象</param>
        /// <returns>-1：存在相同的记录（职位名称相同），0：添加失败，1：添加成功</returns>
        public static int InsertJobTitle(JobTitleInfo jobTitle)
        {
            if (provider.JobTitleExists(jobTitle, OperateType.Insert))
            {   // 判断是否存在相同职位名称的记录
                return -1;
            }

            if (provider.InsertJobTitle(jobTitle))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 修改职位
        /// </summary>
        /// <param name="jobTitle">职位实体对象</param>
        /// <returns>-1：存在相同的记录（职位名称相同），0：修改失败，1：修改成功</returns>
        public static int UpdateJobTitle(JobTitleInfo jobTitle)
        {
            if (provider.JobTitleExists(jobTitle, OperateType.Update))
            {   // 判断是否存在相同职位名称的记录
                return -1;
            }

            if (provider.UpdateJobTitle(jobTitle))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="jobTitleId">职位ID</param>
        /// <returns>删除是否成功（true：成功，false：失败）</returns>
        public static bool DeleteJobTitle(int jobTitleId)
        {
            if (provider.DeleteJobTitle(jobTitleId))
            {
                OnDBChanged();
                OnWriteLog();
                return true;
            }

            return false;
        }

        /// <summary>
        /// 判断职位名称是否存在
        /// </summary>
        /// <param name="jobTitle">职位实体对象</param>
        /// <param name="organizationId">职位所属的部门ID</param>
        /// <param name="type">判断的类型（添加或修改）</param>
        /// <returns>职位名称是否存在（true：已存在，false：不存在）</returns>
        public static bool JobTitleExists(JobTitleInfo jobTitle, OperateType type)
        {
            return provider.JobTitleExists(jobTitle, type);
        }

        /// <summary>
        /// 得到职位实体对象
        /// </summary>
        /// <param name="jobTitleId">职位ID</param>
        /// <returns>职位实体对象</returns>
        public static JobTitleInfo GetJobTitle(int jobTitleId)
        {
            return provider.GetJobTitle(jobTitleId);
        }

        /// <summary>
        /// 得到职位列表
        /// </summary>
        /// <param name="pageId">当前页ID</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="condition">提取的条件</param>
        /// <returns>职位列表</returns>
        public static IList<JobTitleInfo> GetJobTitles(int pageId, int pageSize, string condition)
        {
            IList<JobTitleInfo> jobs = null;

            CacheContext cache = CacheContext.GetCacheService(new DefaultCache());

            if (pageId == 1)
            {
                jobs = cache.RetrieveObject(Cache.CacheKey.JobTitleList.ToString()) as IList<JobTitleInfo>;
                if (jobs == null)
                {
                    jobs = provider.GetJobTitles(pageId, pageSize, condition);
                    cache.AddObject(Cache.CacheKey.JobTitleList.ToString(), jobs);
                }
            }
            else
            {
                jobs = provider.GetJobTitles(pageId, pageSize, condition);
            }

            return jobs;
        }

        /// <summary>
        /// 得到职位列表
        /// </summary>
        /// <param name="condition">提取的条件</param>
        /// <returns>职位列表</returns>
        public static IList<JobTitleInfo> GetJobTitles(string condition)
        {
            return provider.GetJobTitles(condition);
        }

        /// <summary>
        /// 根据职位ID得到职位名称
        /// </summary>
        /// <param name="jobTitleId">职位ID</param>
        /// <returns>职位名称</returns>
        public static string GetJobTitleName(int jobTitleId)
        {
            return provider.GetJobTitleName(jobTitleId);
        }
    }
}
